const express = require("express");
const { ResponseBody } = require("../type");
const { getDatabase, getJsonBySheet, writeDataBase } = require("../database");
const { easyClassConstructor } = require("../util");

const convertMap = {
    name: "名称",
    subject: "主体",
    object: "客体",
    era: "纪元",
    age: "时代",
    year: "年",
    month: "月",
    locale: "地点",
    behavior: "行为",
    result: "结果",
    content: "描述",
};

class TimeEvent {
    constructor(parmas) {
        this.id = parmas.id;
        easyClassConstructor(this, parmas, convertMap);
    }
}

const router = express.Router();

router.get("/get", (req, res) => {
    getDatabase("event")
        .then((data) => {
            const sheet = data.Sheets[data.SheetNames[0]];
            const source = getJsonBySheet(sheet);
            const events = source.map((item) => new TimeEvent(item));
            events.sort((a, b) => {
                return a.startTime > b.startTime;
            });
            res.json(
                new ResponseBody([
                    { name: "星辰纪", data: [] },
                    { name: "星辰纪", data: [] },
                    {
                        name: "星辰纪",
                        data: events,
                    },
                    { name: "星辰纪", data: [] },
                    { name: "星辰纪", data: [] },
                ])
            );
        })
        .catch((err) => {
            res.json(new ResponseBody([], 400, "时间轴请求错误" + err));
        });
});

module.exports = router;
